//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cn.huntercat.decode.util;

import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;

public class SimEncryptUtil {
    private static final String key0 = "EEE123ADCEFDDA";
    private static final Charset charset = Charset.forName("ASCII");
    private static byte[] keyBytes;

    static {
        keyBytes = "EEE123ADCEFDDA".getBytes(charset);
    }

    public SimEncryptUtil() {
    }

    public static String encode(String enc) {
        byte[] b = enc.getBytes(charset);
        int len = b.length;

        int j = 0;
        for (int i = 0; i < len; ++j) {
            if (j == keyBytes.length) {
                j = 0;
            }

            b[i] ^= keyBytes[j];
            ++i;
        }

        StringBuffer buffer = new StringBuffer();

        for (int i = len - 1; i >= 0; --i) {
            if (b[i] > 15) {
                buffer.append(Integer.toHexString(b[i]).toUpperCase());
            } else {
                buffer.append("0").append(Integer.toHexString(b[i]).toUpperCase());
            }
        }

        String tmpStr = buffer.toString();
        return tmpStr;
    }

    public static String decode(String dec) {
        byte[] dee = new byte[dec.length() / 2];
        int len = dee.length;
        int j = 0;

        int i;
        for (i = 0; i < dec.length() / 2; ++i) {
            int index = dec.length() / 2 - 1 - i;
            String str = dec.substring(i * 2, i * 2 + 2);
            dee[index] = (byte) Integer.parseInt(str, 16);
        }

        for (i = 0; i < len; ++j) {
            if (j == keyBytes.length) {
                j = 0;
            }

            dee[i] ^= keyBytes[j];
            ++i;
        }

        return new String(dee);
    }

    public static void main(String[] args) throws Exception {
        String s = "4C809319EF2E990101";
        String enc = encode(s);
//        String dec = decode("057575747176730407077C71700601007575");
        String dec = decode(enc);
        System.out.println("enc==" + enc);
        System.out.println("dec==" + dec);

        // "00 E0 4C 08 DB B7 201004"
        // 4C 80 93 19 EF 2E


    }

    public static String md5(byte[] bs) throws Exception {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(bs);
        String hex = (new BigInteger(1, digest.digest())).toString(16);
        return (new String(new char[32 - hex.length()])).replace("\u0000", "0") + hex;
    }
}
